<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable style="width: 240px">
          <el-option label="启用" value="1" />
          <el-option label="不启用" value="0" />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="备注" prop="remark">-->
        <!--<el-input v-model="queryParams.remark" placeholder="请输入备注" clearable style="width: 240px" @keyup.enter.native="handleQuery" />-->
      <!--</el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="imagesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="80" />
      <el-table-column label="首页图片" align="center" prop="firstUrl" width="120">
        <template slot-scope="scope">
          <div v-if="scope.row.firstUrl">
            <el-image 
              v-for="(url, index) in getTwoUrlArray(scope.row.firstUrl)" 
              :key="index"
              :src="realSrc(url)"
              style="width: 40px; height: 40px; border-radius: 4px; margin: 2px;"
              :preview-src-list="getTwoUrlArray(scope.row.firstUrl).map(url => realSrc(url))"
              fit="cover"
            />
          </div>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="首页中间图片" align="center" prop="twoUrl" width="120">
        <template slot-scope="scope">
          <div v-if="scope.row.twoUrl">
            <el-image 
              v-for="(url, index) in getTwoUrlArray(scope.row.twoUrl)" 
              :key="index"
              :src="realSrc(url)"
              style="width: 40px; height: 40px; border-radius: 4px; margin: 2px;"
              :preview-src-list="getTwoUrlArray(scope.row.twoUrl).map(url => realSrc(url))"
              fit="cover"
            />
          </div>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="首页下面图片" align="center" prop="threeUrl" width="120">
        <template slot-scope="scope">
          <el-image 
            v-if="scope.row.threeUrl" 
            :src="realSrc(scope.row.threeUrl)"
            style="width: 60px; height: 60px; border-radius: 4px;"
            :preview-src-list="[realSrc(scope.row.threeUrl)]"
            fit="cover"
          />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="资料图片" align="center" prop="fourUrl" width="120">
        <template slot-scope="scope">
          <div v-if="scope.row.fourUrl">
            <el-image 
              v-for="(url, index) in getTwoUrlArray(scope.row.fourUrl)" 
              :key="index"
              :src="realSrc(url)"
              style="width: 40px; height: 40px; border-radius: 4px; margin: 2px;"
              :preview-src-list="getTwoUrlArray(scope.row.fourUrl).map(url => realSrc(url))"
              fit="cover"
            />
          </div>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="视频图片" align="center" prop="fiveUrl" width="120">
        <template slot-scope="scope">
          <div v-if="scope.row.fiveUrl">
            <el-image 
              v-for="(url, index) in getTwoUrlArray(scope.row.fiveUrl)" 
              :key="index"
              :src="realSrc(url)"
              style="width: 40px; height: 40px; border-radius: 4px; margin: 2px;"
              :preview-src-list="getTwoUrlArray(scope.row.fiveUrl).map(url => realSrc(url))"
              fit="cover"
            />
          </div>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="工作机会图片" align="center" prop="sixUrl" width="120">
        <template slot-scope="scope">
          <div v-if="scope.row.sixUrl">
            <el-image 
              v-for="(url, index) in getTwoUrlArray(scope.row.sixUrl)" 
              :key="index"
              :src="realSrc(url)"
              style="width: 40px; height: 40px; border-radius: 4px; margin: 2px;"
              :preview-src-list="getTwoUrlArray(scope.row.sixUrl).map(url => realSrc(url))"
              fit="cover"
            />
          </div>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="二维码图片" align="center" prop="sevenUrl" width="120">
        <template slot-scope="scope">
          <el-image 
            v-if="scope.row.sevenUrl" 
            :src="realSrc(scope.row.sevenUrl)" 
            style="width: 60px; height: 60px; border-radius: 4px;"
            :preview-src-list="[realSrc(scope.row.sevenUrl)]"
            fit="cover"
          />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="1"
            inactive-value="0"
            @change="handleStatusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改图片配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="首页图片" prop="firstUrl">
              <image-upload v-model="form.firstUrl" :limit="4" />
              <div class="el-upload__tip">可上传多张图片，最多4张</div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="首页下面图片" prop="threeUrl">
              <image-upload v-model="form.threeUrl" :limit="1" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="二维码图片" prop="sevenUrl">
              <image-upload v-model="form.sevenUrl" :limit="1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="资料图片" prop="fourUrl">
              <image-upload v-model="form.fourUrl" :limit="4" />
              <div class="el-upload__tip">可上传多张图片，最多4张</div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="视频图片" prop="fiveUrl">
              <image-upload v-model="form.fiveUrl" :limit="4" />
              <div class="el-upload__tip">可上传多张图片，最多4张</div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="工作机会图片" prop="sixUrl">
              <image-upload v-model="form.sixUrl" :limit="4" />
              <div class="el-upload__tip">可上传多张图片，最多4张</div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="首页中间图片" prop="twoUrl">
              <image-upload v-model="form.twoUrl" :limit="4" />
              <div class="el-upload__tip">可上传多张图片，最多4张</div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio label="1">启用</el-radio>
                <el-radio label="0">不启用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 图片详情对话框 -->
    <el-dialog title="图片详情" :visible.sync="detailOpen" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="ID">{{ detailForm.id }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="detailForm.status === '1' ? 'success' : 'info'">
            {{ detailForm.status === '1' ? '启用' : '不启用' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="首页图片" :span="2">
          <div v-if="detailForm.firstUrl">
            <el-image 
              v-for="(url, index) in getTwoUrlArray(detailForm.firstUrl)" 
              :key="index"
              :src="realSrc(url)" 
              style="width: 80px; height: 80px; border-radius: 4px; margin: 5px;"
              :preview-src-list="getTwoUrlArray(detailForm.firstUrl).map(url => realSrc(url))"
              fit="cover"
            />
          </div>
          <span v-else>-</span>
        </el-descriptions-item>
        <el-descriptions-item label="首页中间图片" :span="2">
          <div v-if="detailForm.twoUrl">
            <el-image 
              v-for="(url, index) in getTwoUrlArray(detailForm.twoUrl)" 
              :key="index"
              :src="realSrc(url)" 
              style="width: 80px; height: 80px; border-radius: 4px; margin: 5px;"
              :preview-src-list="getTwoUrlArray(detailForm.twoUrl).map(url => realSrc(url))"
              fit="cover"
            />
          </div>
          <span v-else>-</span>
        </el-descriptions-item>
        <el-descriptions-item label="首页下面图片" :span="2">
          <el-image 
            v-if="detailForm.threeUrl" 
            :src="realSrc(detailForm.threeUrl)"
            style="width: 100px; height: 100px; border-radius: 4px;"
            :preview-src-list="[realSrc(detailForm.threeUrl)]"
            fit="cover"
          />
          <span v-else>-</span>
        </el-descriptions-item>
        <el-descriptions-item label="资料图片" :span="2">
          <div v-if="detailForm.fourUrl">
            <el-image 
              v-for="(url, index) in getTwoUrlArray(detailForm.fourUrl)" 
              :key="index"
              :src="realSrc(url)" 
              style="width: 80px; height: 80px; border-radius: 4px; margin: 5px;"
              :preview-src-list="getTwoUrlArray(detailForm.fourUrl).map(url => realSrc(url))"
              fit="cover"
            />
          </div>
          <span v-else>-</span>
        </el-descriptions-item>
        <el-descriptions-item label="视频图片" :span="2">
          <div v-if="detailForm.fiveUrl">
            <el-image 
              v-for="(url, index) in getTwoUrlArray(detailForm.fiveUrl)" 
              :key="index"
              :src="realSrc(url)" 
              style="width: 80px; height: 80px; border-radius: 4px; margin: 5px;"
              :preview-src-list="getTwoUrlArray(detailForm.fiveUrl).map(url => realSrc(url))"
              fit="cover"
            />
          </div>
          <span v-else>-</span>
        </el-descriptions-item>
        <el-descriptions-item label="工作机会图片" :span="2">
          <div v-if="detailForm.sixUrl">
            <el-image 
              v-for="(url, index) in getTwoUrlArray(detailForm.sixUrl)" 
              :key="index"
              :src="realSrc(url)" 
              style="width: 80px; height: 80px; border-radius: 4px; margin: 5px;"
              :preview-src-list="getTwoUrlArray(detailForm.sixUrl).map(url => realSrc(url))"
              fit="cover"
            />
          </div>
          <span v-else>-</span>
        </el-descriptions-item>
        <el-descriptions-item label="二维码图片" :span="2">
          <el-image 
            v-if="detailForm.sevenUrl" 
            :src="realSrc(detailForm.sevenUrl)" 
            style="width: 100px; height: 100px; border-radius: 4px;"
            :preview-src-list="[realSrc(detailForm.sevenUrl)]"
            fit="cover"
          />
          <span v-else>-</span>
        </el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ detailForm.remark || '-' }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ parseTime(detailForm.createTime) }}</el-descriptions-item>
        <el-descriptions-item label="更新时间">{{ parseTime(detailForm.updateTime) }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
      </div>
  </template>

<script>
import { listImages, getImages, addImages, updateImages, delImages, updateImagesStatus } from "@/api/system/images";
import ImageUpload from "@/components/ImageUpload";
import { isExternal } from '@/utils/validate'

export default {
  name: "Images",
  components: {
    ImageUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 图片配置表格数据
      imagesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      detailOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        remark: null
      },
      // 表单参数
      form: {},
      // 详情表单参数
      detailForm: {},
      // 表单校验
      rules: {
//        firstUrl: [
//          { required: true, message: "首页图片不能为空", trigger: "blur" }
//        ],
//        twoUrl: [
//          { required: true, message: "首页中间图片不能为空", trigger: "blur" }
//        ],
//        threeUrl: [
//          { required: true, message: "首页下面图片不能为空", trigger: "blur" }
//        ],
//        fourUrl: [
//          { required: true, message: "资料图片不能为空", trigger: "blur" }
//        ],
//        fiveUrl: [
//          { required: true, message: "视频图片不能为空", trigger: "blur" }
//        ],
//        sixUrl: [
//          { required: true, message: "工作机会图片不能为空", trigger: "blur" }
//        ],
//        sevenUrl: [
//          { required: true, message: "二维码图片不能为空", trigger: "blur" }
//        ],
//        status: [
//          { required: true, message: "状态不能为空", trigger: "change" }
//        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    realSrc(src) {
      if (!src) {
        return
      }
      let real_src = src.split(",")[0]
      if (isExternal(real_src)) {
        return real_src
      }
      return process.env.VUE_APP_BASE_API + real_src
    },
    /** 查询图片配置列表 */
    getList() {
      this.loading = true;
      listImages(this.queryParams).then(response => {
        this.imagesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        firstUrl: null,
        twoUrl: null,
        threeUrl: null,
        fourUrl: null,
        fiveUrl: null,
        sixUrl: null,
        sevenUrl: null,
        status: "1",
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加图片配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids[0];
      getImages(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改图片配置";
      });
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id;
      getImages(id).then(response => {
        this.detailForm = response.data;
        this.detailOpen = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 处理多图片字段数组转字符串
          const multiImageFields = ['firstUrl', 'twoUrl', 'fourUrl', 'fiveUrl', 'sixUrl'];
          multiImageFields.forEach(field => {
            if (this.form[field] && Array.isArray(this.form[field])) {
              this.form[field] = this.form[field].join(',');
            }
          });
          
          if (this.form.id != null) {
            updateImages(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addImages(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除图片配置编号为"' + ids + '"的数据项？').then(function() {
        return delImages(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 状态修改 */
    handleStatusChange(row) {
      let text = row.status === "1" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.id + '"图片配置吗？').then(function() {
        return updateImagesStatus({ id: row.id, status: row.status });
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    /** 将twoUrl字符串转换为数组 */
    getTwoUrlArray(twoUrl) {
      if (!twoUrl) return [];
      if (Array.isArray(twoUrl)) return twoUrl;
      return twoUrl.split(',').filter(url => url.trim());
    }
  }
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.mb8 {
  margin-bottom: 8px;
}

.el-upload__tip {
  font-size: 12px;
  color: #606266;
  margin-top: 5px;
}

.dialog-footer {
  text-align: right;
}

/* 图片预览样式 */
.el-image {
  border: 1px solid #ebeef5;
  transition: all 0.3s;
}

.el-image:hover {
  border-color: #409eff;
  transform: scale(1.05);
}

/* 表格中的图片容器 */
.el-table .cell {
  text-align: center;
}

/* 状态开关样式 */
.el-switch {
  margin: 0 auto;
}

/* 详情页面的图片展示 */
.el-descriptions-item__content .el-image {
  margin: 5px;
}
</style>
